import numpy as np  
from PIL import Image  
import imageio  
  
# 读取图像并转换为NumPy数组  
image1_path = "C:\\Users\\lenovo\\Desktop\\Math\\Trump.png"  
image2_path = "C:\\Users\\lenovo\\Desktop\\Math\\shoot.png"  
  
image1 = Image.open(image1_path).convert('RGB')  
image2 = Image.open(image2_path).convert('RGB')  
  
# 确保尺寸相同  
assert image1.size == image2.size, "图像必须有相同的尺寸"  
  
# 将图像转换为NumPy数组  
image1_array = np.array(image1)  
image2_array = np.array(image2)  
  
# 获取图像的尺寸  
height, width, channels = image1_array.shape  
  
# 设置动画的帧数  
num_frames = 50  
  
# 创建空列表来存储每一帧的图像  
frames = []  
  
# 使用线性插值生成每一帧的图像  
for alpha in np.linspace(0, 1, num_frames):  
    interpolated_image = (1 - alpha) * image1_array + alpha * image2_array  
    interpolated_image = np.clip(interpolated_image, 0, 255).astype(np.uint8)  # 确保像素值在0-255之间  
    frame = Image.fromarray(interpolated_image)#将NumPy数组转换为Pillow图像  
    frames.append(frame)  
  
# 使用imageio将帧列表保存为GIF文件  
imageio.mimsave('transition_animation.gif', frames, duration=0.1)  # duration参数设置每帧之间的时间间隔（秒）
